﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL;
using System.IO;
using System.Data;
using BLL.Entities;

namespace CorporateGymManagementSystem
{

    /// <summary>
    /// Interaction logic for ReportsWindow.xaml
    /// </summary>
    public partial class ReportsWindow : Window
    {
        public IReport Report { get; set; }
        private int currentReportMonth;
        private int currentReportYear;
        public ReportsWindow()
        {
            InitializeComponent();
        }

        private void exportButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = string.Format("Montly Report For {2} {0}-{1}", currentReportMonth, currentReportYear, Report is ShiftsReport? "Shifts" : "Visits"); // Default file name
            dlg.DefaultExt = ".csv"; // Default file extension
            dlg.Filter = "Text documents (.csv)|*.csv"; // Filter files by extension

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                string filename = dlg.FileName;
                CSVCreator creator = new CSVCreator();
                if (Report is ShiftsReport)
                {
                    creator.ExportShiftsToCSV((Report as ShiftsReport).shifts, filename);
                }
                else
                {
                    creator.ExportVisitsToCSV((Report as VisitsReport).visits, filename);
                }
                
            }

            
        }

        private void createReportButton_Click(object sender, RoutedEventArgs e)
        {
            switch (comboBoxReportType.SelectedIndex)
	        {
                case 1:
                    CreateShiftsReport();
                    exportButton.IsEnabled = true;
                    break;
                case 2:
                    CreateVisitsReport();
                    exportButton.IsEnabled = true;
                    break;
		        default:
                    break;
	        }
        }

        public void OnExportGridToCSV(object sender, System.EventArgs e)
        {
            // Create the CSV file to which grid data will be exported.
            //StreamWriter sw = new StreamWriter(Server.MapPath("~/GridData.csv"), false);
            StreamWriter sw = new StreamWriter("GridData.csv");
            // First we will write the headers.
            DataTable dt = ((DataSet)reportGrid.DataContext).Tables[0];
            int iColCount = dt.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                sw.Write(dt.Columns[i]);
                if (i < iColCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
            // Now write all the rows.
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        sw.Write(dr[i].ToString());
                    }
                    if (i < iColCount - 1)
                    {
                        sw.Write(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);
                    }
                }
                sw.Write(sw.NewLine);
            }
            sw.Close();
        }

        private void CreateShiftsReport()
        {
            reportGrid.Columns[2].Visibility = Visibility.Visible;
            reportGrid.Columns[1].Header = "Shift Start At";
            currentReportMonth = int.Parse(comboBoxMonth.Text);
            currentReportYear = int.Parse(comboBoxYear.Text);
            Report = new ShiftsReport();
            Report.Create(currentReportMonth, currentReportYear);
            List<ShiftDataRow> data = new List<ShiftDataRow>();
            foreach (KeyValuePair<GymWorker, List<Shift>> entry in (Report as ShiftsReport).shifts)
            {
                foreach (var shift in entry.Value)
                {
                    data.Add(new ShiftDataRow
                    {
                        Name = entry.Key.FullName,
                        start = shift.StartingHour,
                        end = shift.EndingHour
                    });
                }
            }
            reportGrid.Columns.Last().Visibility = Visibility.Visible;
            DataContext = data;
            
        }

        private void CreateVisitsReport()
        {
            reportGrid.Columns[2].Visibility = Visibility.Hidden;
            reportGrid.Columns[1].Header = "Visit At";
            currentReportMonth = int.Parse(comboBoxMonth.Text);
            currentReportYear = int.Parse(comboBoxYear.Text);
            Report = new VisitsReport();
            Report.Create(currentReportMonth, currentReportYear);
            List<VisitDataRow> data = new List<VisitDataRow>();
            foreach (KeyValuePair<Trainee, List<GymVisit>> entry in (Report as VisitsReport).visits)
            {
                foreach (var visit in entry.Value)
                {
                    data.Add(new VisitDataRow
                    {
                        Name = entry.Key.FullName,
                        start = visit.Start,
                        end = DateTime.Now
                    });
                }
            }
            reportGrid.Columns.Last().Visibility = Visibility.Hidden;
            DataContext = data;
            
        }
    }

    public class ShiftDataRow
    {
        public string Name { set; get; }
        public DateTime start { set; get; }
        public DateTime end { set; get; }
    }

    public class VisitDataRow
    {
        public string Name { set; get; }
        public DateTime start { set; get; }
        public DateTime end { set; get; }
    }
}
